package edu.jiefengli.course_selection_system.service.impl;

import edu.jiefengli.course_selection_system.mapper.CourseRecordMapper;
import edu.jiefengli.course_selection_system.pojo.entity.CourseRecord;
import edu.jiefengli.course_selection_system.pojo.entity.User;
import edu.jiefengli.course_selection_system.service.CourseRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseRecordServiceImpl implements CourseRecordService {

    @Autowired
    private CourseRecordMapper courseRecordMapper;

    @Override
    public List<CourseRecord> getAllRecords() {
        return courseRecordMapper.getAllRecords();
    }

    @Override
    public boolean addRecord(Integer studentId, Integer courseId) {
       int cnt = courseRecordMapper.isSelectedCourseRecord(studentId, courseId);
       if (cnt == 0) {
           courseRecordMapper.addCourseRecord(studentId, courseId);
           return true;
       } else {
           return false;
       }
    }

    @Override
    public List<CourseRecord> getCourseRecordsById(Integer studentId) {
        return courseRecordMapper.getCourseRecordsById(studentId);
    }

    @Override
    public void deleteRecord(Integer studentId, Integer courseId) {
        courseRecordMapper.removeCourseRecordById(studentId, courseId);
    }

    @Override
    public List<User> getSelectedCourseStudent(int courseId) {
        return courseRecordMapper.getSelectedCourseStudent(courseId);
    }
}
